<template>
  <div class="big-box">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>修改密码</span>
      </div>
      <div class="text item">
        <el-form ref="form" :model="form" label-width="80px" :rules="rules">
          <el-form-item label="用户名" prop="username">
            <el-input type="text" v-model="username" disabled></el-input>
          </el-form-item>
          <el-form-item label="旧密码" prop="oldpassword">
            <el-input show-password v-model="form.oldpassword"></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="newpassword">
            <el-input show-password v-model="form.newpassword"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="repassowrd">
            <el-input show-password v-model="form.repassowrd"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('form')"
              >立即修改</el-button
            >
            <el-button @click="resetForm('form')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  data() {
    var checkpassord = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入旧密码"));
      } else if (value !== this.password) {
        callback(new Error("密码不正确"));
      } else {
        callback();
      }
    };
    var checkpassword1 = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入密码"));
      } else {
        if (this.form.repassword) {
          this.$refs.form.validateField("repassword");
        }
        callback();
      }
    };
    var checkpassword2 = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请再次输入密码"));
      } else if (this.form.newpassword !== value) {
        callback(new Error("两次密码不一致"));
      } else {
        callback();
      }
    };
    return {
      form: {
        oldpassword: "",
        newpassword: "",
        repassword: "",
      },
      rules: {
        oldpassword: [
          { required: true, message: "请输入旧密码", trigger: "blur" },
          { validator: checkpassord, trigger: "blur" },
        ],
        newpassword: [
          { required: true, message: "请输入新密码", trigger: "blur" },
          { validator: checkpassword1, trigger: "blur" },
        ],
        repassowrd: [
          { required: true, message: "请输入确认密码", trigger: "blur" },
          { validator: checkpassword2, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$store.dispatch("modifyInfo", this.form)
          this.$store.dispatch("logout")
          this.$router.push("/login")
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  computed: {
    ...mapState({
      username: (state) => state.user.userInfo.username,
      password: (state) => state.user.userInfo.password,
    }),
  },
};
</script>

<style lang="less" scoped>
.big-box {
  width: 90vw;
  height: 80vh;
  box-sizing: border-box;
}
.el-card {
  position: relative;
  top: 10%;
  left: 35%;
  height: 400px;
  width: 400px;
}
</style>
